package com.bs.service.impl;

import java.util.*;
import com.bs.bean.Comment;
import com.bs.service.CommentService;
import com.bs.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
/*业务层：CommentService接口的实现类，调用CommentDao接口的实现类对数据进行相关业务等操作*/
public class CommentServiceImpl implements CommentService{

//	注入CommentDao接口的实现类对象
	@Autowired
	CommentMapper commentDao;
//	根据条件查询comment中的数据
	public List<Comment> findAll(Comment query) {
		// TODO Auto-generated method stub
		List<Comment> list = findAll(query==null?new Comment():query,true);
		
		
		
		Iterator<Comment> iterator = list.iterator();
		while (iterator.hasNext()) {
			Comment Comment = (Comment) iterator.next();
			if (Comment.getReplyid()!=null) {
				iterator.remove();
			}
		}
		for (Comment Comment : list) {
			name(Comment);
		}
		for (Comment Comment : list) {
			System.out.println(Comment);
		}
		return list;
	}
	
//	根据条件和模式查询comment中的数据
	public List<Comment> findAll(Comment whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Comment> list = commentDao.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<Comment> iterator = list.iterator();
			while (iterator.hasNext()) {
				Comment comment = (Comment) iterator.next();
				if (whereWrap.getId() != null&&(!(whereWrap.getId()+"").equals(""))) {
					if (mode) {
						if (!(comment.getId() + "").equals(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(comment.getId() + "").contains(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getInstrumentid() != null&&(!(whereWrap.getInstrumentid()+"").equals(""))) {
					if (mode) {
						if (!(comment.getInstrumentid() + "").equals(whereWrap.getInstrumentid() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(comment.getInstrumentid() + "").contains(whereWrap.getInstrumentid() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getUser() != null&&(!(whereWrap.getUser()+"").equals(""))) {
					if (mode) {
						if (!(comment.getUser() + "").equals(whereWrap.getUser() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(comment.getUser() + "").contains(whereWrap.getUser() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getContent() != null&&(!(whereWrap.getContent()+"").equals(""))) {
					if (mode) {
						if (!(comment.getContent() + "").equals(whereWrap.getContent() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(comment.getContent() + "").contains(whereWrap.getContent() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getReplyid() != null&&(!(whereWrap.getReplyid()+"").equals(""))) {
					if (mode) {
						if (!(comment.getReplyid() + "").equals(whereWrap.getReplyid() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(comment.getReplyid() + "").contains(whereWrap.getReplyid() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		
		return list;
	}
	
	public void name(Comment comment) {
		List<Comment> replyComments = findAll(new Comment().setReplyid(comment.getId()),true);
		if (replyComments.size()==0) {
			return;
		}
		comment.setComments(replyComments);
		for (Comment replyComment : replyComments) {
			name(replyComment);
		}
	}
	
//	查询comment中的数据
	public List<Comment> findAll() {
		// TODO Auto-generated method stub
		return findAll(new Comment());
	}
	
//	根据主键查询comment中的一条数据
	public Comment findById(Integer id) {
		// TODO Auto-generated method stub
		return commentDao.selectByPrimaryKey(id);
	}

//	选择性添加comment表中的comment数据
	public int insert(Comment comment) {
		// TODO Auto-generated method stub
		return commentDao.insertSelective(comment);
	}

//	根据主键更新comment表中的一条记录
	public int updateById(Comment comment) {
		// TODO Auto-generated method stub
		return commentDao.updateByPrimaryKey(comment);
	}

//	根据主键选择性更新comment表中的comment数据
	public int updateByIdSelective(Comment comment) {
		// TODO Auto-generated method stub
		Comment dbComment = findById(comment.getId());
		if (comment.getId()==null) {
			comment.setId(dbComment.getId());
		}
		if (comment.getInstrumentid()==null) {
			comment.setInstrumentid(dbComment.getInstrumentid());
		}
		if (comment.getUser()==null) {
			comment.setUser(dbComment.getUser());
		}
		if (comment.getContent()==null) {
			comment.setContent(dbComment.getContent());
		}
		if (comment.getReplyid()==null) {
			comment.setReplyid(dbComment.getReplyid());
		}
		return updateById(comment);
	}

//	根据条件删除comment表中的数据
	public int delete(Comment whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap,true);
	}

//	根据条件和模式删除comment表中的数据
	public int delete(Comment whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Comment> list = findAll(whereWrap, mode);
		for (Comment comment : list) {
			deleteById(comment.getId());
		}
		return list.size();
	}
//	根据主键删除comment表中的一条记录
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return commentDao.deleteByPrimaryKey(id);
	}

//	根据主键集合批量删除comment表中的记录
	public int deleteByIds(List<Integer> list){
		for (Integer id : list) {
			deleteById(id);
		}
		return list.size();
	}
}
